import 'package:flutter_boost/flutter_boost.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:libflutter/net/RDError.dart';

///
abstract class RDOnSuperError {
  onSuperError(Error error);
}

abstract class RDOnError implements RDOnSuperError {
  onError(Error error);
}

class _RDOnErrorCallSuper implements RDOnError {
  _RDOnErrorCallSuper({required void Function(Error, RDOnSuperError) onError}) : _onError = onError;

  final void Function(Error, RDOnSuperError) _onError;

  @override
  onError(Error error) {
    onSuperError(error);
    _onError(error, this);
  }

  // 进行集中处理
  @override
  void onSuperError(Error error) {
    if (error is RDError) {
      switch (error.code) {
        case RDError.CODE_300:
          Logger.error("请求失败");
          Fluttertoast.showToast(msg: "错误代码:${error.code}");
          break;
        default:
          Fluttertoast.showToast(msg: error.msg);
      }
    }
  }
}

class _RDOnError extends _RDOnErrorCallSuper {
  _RDOnError({required super.onError});

  @override
  onError(Error error) {
    _onError(error, this);
  }
}

RDOnError createCallSuper({required void Function(Error, RDOnSuperError) onError}) {
  return _RDOnErrorCallSuper(onError: onError);
}

RDOnError createIgnoreSuper({required void Function(Error, RDOnSuperError) onError}) {
  return _RDOnError(onError: onError);
}
